//
//  EnergySliderView.swift
//  SCA
//
//  Created by tjun on 2025/9/7.
//

import UIKit
import SnapKit

class EnergySliderView: UIView {
    
    var maxValue: Double = 0 {
        didSet { updateUI() }
    }
    
    var currentValue: Double = 0 {
        didSet {
            updateUI()
            valueLabel.text = "\(Int(currentValue))/\(Int(maxValue))"
        }
    }
    
    private let titleLabel = UILabel(text: "活力值".localizedString, font: UIFontMediumMake(12), textColor: .white)
    private let valueLabel = UILabel(text: "0/0", font: UIFontMake(12), textColor: .white)

    private let trackBackground = UIView()
    private let progressView = UIView()
    private let thumbView: UIView = {
        let view = UIView()
        
        let icon = UIImageView(image: UIImage(systemName: "bolt.fill"))
        icon.tintColor = .white
        icon.contentMode = .scaleAspectFit
        view.addSubview(icon)
        
        icon.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(12)
        }
        return view
    }()
    
    var onInfoButtonTapped: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        currentValue = 12
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {

        titleLabel.addTo(self)
        titleLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
        }
        
        valueLabel.addTo(self)
        valueLabel.snp.makeConstraints { make in
            make.centerY.equalTo(titleLabel)
            make.right.equalToSuperview()
        }
        
        let infoButton = UIButton(type: .system).addTo(self)
        infoButton.addTarget(self, action: #selector(infoButtonTapped), for: .touchUpInside)
        infoButton.setImage(UIImage(named: "more-info-icon"), for: .normal)
        infoButton.snp.makeConstraints { make in
            make.width.height.equalTo(20)
            make.left.equalTo(titleLabel.snp.right).offset(20)
            make.centerY.equalTo(titleLabel)
        }

        let gradient = CAGradientLayer()
        gradient.colors = [UIColor.cyan.cgColor, UIColor.purple.cgColor, UIColor.magenta.cgColor]
        gradient.startPoint = CGPoint(x: 0, y: 0.5)
        gradient.endPoint = CGPoint(x: 1, y: 0.5)
        gradient.cornerRadius = 2
        progressView.layer.addSublayer(gradient)
        
        trackBackground.addTo(self)
        trackBackground.addSubview(progressView)
        trackBackground.addSubview(thumbView)
        trackBackground.backgroundColor = UIColor.gray.withAlphaComponent(0.3)
        trackBackground.layer.cornerRadius = 2
        trackBackground.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.right.equalToSuperview()
            make.height.equalTo(8)
        }

        progressView.snp.makeConstraints { make in
            make.top.bottom.left.equalToSuperview()
            make.width.equalTo(0)
        }
        
        thumbView.backgroundColor = UIColorMake(0xE904D8)
        thumbView.addCorner(radius: 10)
        thumbView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.size.equalTo(20)
            make.left.equalTo(progressView.snp.right).offset(-10)
        }
    }
    
    @objc func infoButtonTapped() {
        onInfoButtonTapped?()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if let gradient = progressView.layer.sublayers?.first as? CAGradientLayer {
            gradient.frame = progressView.bounds
        }
    }
    
    private func updateUI() {
        
        let rawPercent = maxValue == 0 ? 0 : currentValue / maxValue
        let percent = CGFloat(min(max(rawPercent, 0), 1))
         let totalWidth = trackBackground.bounds.width
         let progressWidth = totalWidth * percent

         progressView.snp.updateConstraints { make in
             make.width.equalTo(progressWidth)
         }

         layoutIfNeeded()
     }
}
